package com.example.demo.account.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.account.entity.TblAccount;
import com.example.demo.account.mapper.TblAccountMapper;
import com.example.demo.account.service.ITblAccountService;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zgq
 * @since 2020-12-07
 */
@Slf4j
@Service
public class TblAccountServiceImpl extends ServiceImpl<TblAccountMapper, TblAccount> implements ITblAccountService {

    @Autowired
    TblAccountMapper accountMapper;

    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int decAccount(String userId, int amt) {
        log.info("TblAccountServiceImpl->decAccount  xid:{}", RootContext.getXID());
        int ret = accountMapper.decAccount(userId, amt);
        if(amt==100)
            throw new RuntimeException("人工异常测试");

        try {
            //测试超过时长
            if(amt==50)
                Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        return ret;
    }
}
